package club.ljwang.wechat;

import club.ljwang.constant.MessageConstant;
import club.ljwang.constant.RedisConstant;
import club.ljwang.constant.RedisMessageConstant;
import club.ljwang.entity.Result;
import club.ljwang.utils.JedisUtil;
import club.ljwang.utils.SmsUtil;
import club.ljwang.utils.ValidateCodeUtils;
import cn.hutool.core.lang.Assert;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.util.Map;

/**
 * @Auther: wangl
 * @Date: 2020/3/3 23:48
 * @Description:
 */

@RestController
@RequestMapping("validateCode")
public class ValidateCodeController {

    @Autowired
    private JedisUtil jedisUtil;


    @Autowired
    private DefaultKaptcha defaultKaptcha;

    @RequestMapping("/code/{deviceId}")
    public void createCode(@PathVariable String deviceId, HttpServletResponse response) throws Exception {
        Assert.notNull(deviceId, "机器码不能为空");
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        //生成文字验证码
        String text = defaultKaptcha.createText();
        //生成图片验证码
        BufferedImage image = defaultKaptcha.createImage(text);
        //生成的验证码写入redis

        jedisUtil.setex(deviceId,60*5,text);
        //获取输出流
        ServletOutputStream out = response.getOutputStream();
        //将图片写回浏览器
        ImageIO.write(image, "JPEG", out);
    }

    @RequestMapping("send4Order")
    public Result send4Order(String telephone) {

        Integer code = ValidateCodeUtils.generateValidateCode(6);
        SmsUtil.sendSmsCode(telephone,String.valueOf(code));

        jedisUtil.setex(telephone + RedisMessageConstant.SENDTYPE_ORDER, 60 * 5 * 10, String.valueOf(code));


        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);

    }


    @RequestMapping("/send4Login")
    public Result send4Login(@RequestBody Map<String,String> map){
        String telephone = map.get("telephone");
        String deviceId = map.get("deviceId");
        String imgCode = map.get("code");

        String redisImgCode = jedisUtil.get(deviceId);
        if(null == redisImgCode || !redisImgCode.equals(imgCode)){
            return new Result(false ,"图形验证码输入错误");
        }
        //生成验证码
        Integer code = ValidateCodeUtils.generateValidateCode(6);
        //调用短信接口发送验证
        SmsUtil.sendSmsCode(telephone,String.valueOf(code));
        System.out.println("code:" + code);

        jedisUtil.setex( telephone+RedisConstant.SENDTYPE_LOGIN ,60 * 60 * 24,String.valueOf(code));

        return new Result(true,"");
    }
}

